﻿@model ZDSoft.TES.Entity.SystemEntity.Role
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Create</title>
    <script src='@Url.Content("~/Scripts/PageCreater/Scripts/jquery-1.5.1.min.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/PageCreater/Scripts/PageCreater.js")' type="text/javascript"></script>
    <link href='@Url.Content("~/Scripts/PageCreater/Css/Style.css")' rel="stylesheet" type="text/css" />
    <script src='@Url.Content("~/Scripts/zTree/js/jquery-1.4.4.min.js")' type="text/javascript"></script>
    <link href='@Url.Content("~/Scripts/zTree/css/zTreeStyle/zTreeStyle.css")' rel="stylesheet" type="text/css" />
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.core-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.excheck-3.5.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/Scripts/zTree/js/jquery.ztree.exedit-3.5.js")' type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.ajaxSetup({ cache: false });
            var str = "RoleName,角色名,false,text,notnull,"
                        + "IsActive,状态,false,select|激活!true|注销!false,notnull,"
                        + "Opreate,操作,false,null,null";
            CreatePageFram(str);
            $.fn.zTree.init($("#systemFunctionTree"), sysSetting, sysNodes);
        })

        var sysSetting = {
            async: {
                enable: true,
                url: "/Role/GetAllSystemFunction",
                type: "Get",
                dataType: "json",
                autoParam: ["id","pid","name","level"],  
            },
            check: {
                enable: true
            },
            callback: {
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                },
                onAsyncError: function (event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
                 
                    alert("加载错误，请联系系统管理员！");
                },
                beforeExpand: beforeExpand,
                onExpand: onExpand,
                onClick: onClick,
                onCheck: function (event, treeId, treeNode) {
                    if (treeNode.checked == true) {
                        if (treeNode.level == 0) {
                            if (treeNode.children == null) {
                                treeNode.checked = false;
                                alert("请先展开列表！");
                            }
                        }
                    }
                }
            }
        };
        function createNodes(maxNodesNumInLevel, maxLevel, curLevel, curPId) {
            if (maxNodesNumInLevel < 5) {
                maxNodesNumInLevel = 5;
            }
            var nodes = [], num = 0;
            while (num < 3) {
                num = parseInt(Math.random() * 1024) % maxNodesNumInLevel + 1;
            }
            for (var i = 0; i < num; i++) {
                var id = curPId ? curPId + "-" + i : "" + i, isParent = (parseInt(Math.random() * 9999) % 3 != 0),
				node = { id: id, pId: curPId, name: "N" + id };
                nodes.push(node);
                if (isParent && curLevel < maxLevel) {
                    nodes = nodes.concat(createNodes(maxNodesNumInLevel, maxLevel, curLevel + 1, id));
                }
            }
            return nodes;
        }
        var zNodes = createNodes(5, 5, 0);

        var curExpandNode = null;
        function beforeExpand(treeId, treeNode) {
            var pNode = curExpandNode ? curExpandNode.getParentNode() : null;
            var treeNodeP = treeNode.parentTId ? treeNode.getParentNode() : null;
            var zTree = $.fn.zTree.getZTreeObj("systemFunctionTree");
            for (var i = 0, l = !treeNodeP ? 0 : treeNodeP.children.length; i < l; i++) {
                if (treeNode !== treeNodeP.children[i]) {
                    zTree.expandNode(treeNodeP.children[i], false);
                }
            }
            while (pNode) {
                if (pNode === treeNode) {
                    break;
                }
                pNode = pNode.getParentNode();
            }
            if (!pNode) {
                singlePath(treeNode);
            }

        }
        function singlePath(newNode) {
            if (newNode === curExpandNode) return;
            if (curExpandNode && curExpandNode.open == true) {
                var zTree = $.fn.zTree.getZTreeObj("systemFunctionTree");
                if (newNode.parentTId === curExpandNode.parentTId) {
                    zTree.expandNode(curExpandNode, false);
                } else {
                    var newParents = [];
                    while (newNode) {
                        newNode = newNode.getParentNode();
                        if (newNode === curExpandNode) {
                            newParents = null;
                            break;
                        } else if (newNode) {
                            newParents.push(newNode);
                        }
                    }
                    if (newParents != null) {
                        var oldNode = curExpandNode;
                        var oldParents = [];
                        while (oldNode) {
                            oldNode = oldNode.getParentNode();
                            if (oldNode) {
                                oldParents.push(oldNode);
                            }
                        }
                        if (newParents.length > 0) {
                            zTree.expandNode(oldParents[Math.abs(oldParents.length - newParents.length) - 1], false);
                        } else {
                            zTree.expandNode(oldParents[oldParents.length - 1], false);
                        }
                    }
                }
            }
            curExpandNode = newNode;
        }

        function onExpand(event, treeId, treeNode) {
            curExpandNode = treeNode;
        }

        function onClick(e, treeId, treeNode) {
            if (treeNode.level == 0) {
                treeNode.checked = true;
            }
            var zTree = $.fn.zTree.getZTreeObj("systemFunctionTree");
            zTree.expandNode(treeNode, null, null, null, true);
        }
        var sysNodes = [
			{ id: 1, pid: 0, name: "请选择配置功能信息", isParent: true, icon: "../../Scripts/zTree/css/zTreeStyle/img/diy/users.png" }
		];

        function SubmitPage() {
            if (!InitValidator()) {
                return false;
            }
            var node = $.fn.zTree.getZTreeObj("systemFunctionTree").getNodeByParam("id", 1, null);
            if (node.checked == false) {
                alert("请先展开角色信息并确认！");
                return false;
            }
            var nodes = $.fn.zTree.getZTreeObj("systemFunctionTree").getCheckedNodes(true);
            var selectIDs = "";
            for (var i = 0; i < nodes.length; i++) {
                var xxx = nodes[i];
                selectIDs += nodes[i].tld + ",";
            }
            $.ajax({
                type: "Post",
                url: "/Role/Create",
                data: {RoleName: $("#RoleName").val(), isActive: $("#IsActive").val(), selectedIds: selectIDs },
                success: function () {
                    alert("添加成功！");
                    window.location = "/Role/Index";
                }
            });
        }
    </script>
</head>
<body>
    <div class="Content">
        <p>
            @Html.ActionLink("返回列表", "Index")
        </p>
        <p>
            <input id="btnSubmit" type="button" value="保存" class="btn3" onclick="SubmitPage()" /></p>
        <form id="Info" method="post">
        <div style="float: left; width: 30%;">
            <fieldset style="border: none;">
                <legend>请输入基本信息</legend>
                <div id="divContent">
                </div>
            </fieldset>
            <div style="float: left; width: 30%;">
                <fieldset style="border: none;">
                    <legend>请配置功能信息</legend>
                    <ul id="systemFunctionTree" class="ztree">
                    </ul>
                </fieldset>
            </div>
        </div>
        </form>
    </div>
</body>
</html>
